package io.wongxd.solution.compose.custom.views.chain_scrollable_component

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.unit.Velocity
import io.wongxd.solution.compose.custom.views.util.midOf

/**
 * effect : 对应[ChainMode.ChainContentFirst]的[NestedScrollConnection]
 * warning:
 */
internal class ChainContentFirstNestedScrollConnection(
    val state: ChainScrollableComponentState,
) : NestedScrollConnection {
    override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
        val delta = if (state.orientationIsHorizontal) available.x else available.y
        val newOffset = state.getScrollPositionValue() + delta
        state.setScrollPosition(midOf(state.minPx, newOffset, state.maxPx))
        return Offset.Zero
    }

    override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
        state.callOnScrollStop()
        return super.onPostFling(consumed, available)
    }
}